package com.tripspins.app.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.tripspins.app.bo.CurrentWeather;


/**
 * Created by João.Rutkoski on 06/09/13.
 */
public class CurrentWeatherDAO {

    private SQLiteDatabase database;

    public CurrentWeatherDAO(SQLiteDatabase database)
    {
        this.database = database;
    }
    public void persist(long pinId, CurrentWeather weather) {
        if(getCurrentWeather(pinId)==null)
            insertCurrentWeather(pinId, weather);
        else 
            updateCurrentWeather(pinId);
        
    }

    private void updateCurrentWeather(long pinId) {
        StringBuilder sb = new StringBuilder();
        sb.append("Update PIN_WEATHER set txt_synced = 'Y' ");
        sb.append( "WHERE id_pin = ? ");
        database.execSQL(sb.toString(),new Object[]{pinId});
    }

    public void insertCurrentWeather(long pinId, CurrentWeather weather)
    {
        StringBuilder sbInsert = new StringBuilder();
        sbInsert.append("INSERT INTO PIN_WEATHER ( id_pin " );
        sbInsert.append(                         ",num_cloud_cover " );
        sbInsert.append(                         ",num_humidity " );
        sbInsert.append(                         ",num_precipitation_mm " );
        sbInsert.append(                         ",num_pressure " );
        sbInsert.append(                         ",num_temp_c " );
        sbInsert.append(                         ",num_temp_f " );
        sbInsert.append(                         ",num_visibility " );
        sbInsert.append(                         ",txt_wind_dir_16Point " );
        sbInsert.append(                         ",txt_wind_dir_degree " );
        sbInsert.append(                         ",num_wind_speed_kmph " );
        sbInsert.append(                         ",num_wind_speed_mph " );
        sbInsert.append(                         ",txt_description " );
        sbInsert.append(                         ",txt_icon_url )");
        sbInsert.append(                 "VALUES ( ?");//id_pin
        sbInsert.append(                        ", ?");//num_cloud_cover
        sbInsert.append(                        ", ?");//num_humidity
        sbInsert.append(                        ", ?");//num_precipitation_mm
        sbInsert.append(                        ", ?");//num_pressure
        sbInsert.append(                        ", ?");//num_temp_c
        sbInsert.append(                        ", ?");//num_temp_f
        sbInsert.append(                        ", ?");//num_visibility
        sbInsert.append(                        ", ?");//txt_wind_dir_16Point
        sbInsert.append(                        ", ?");//txt_wind_dir_degree
        sbInsert.append(                        ", ?");//num_wind_speed_kmph
        sbInsert.append(                        ", ?");//num_wind_speed_mph
        sbInsert.append(                        ", ?");//txt_description
        sbInsert.append(                        ", ?);");//txt_icon_url

        database.execSQL(sbInsert.toString(),new Object[]{
                pinId,
                weather.getCloudCover(),
                weather.getHumidity(),
                weather.getPrecipMM(),
                weather.getPressure(),
                weather.getTempC(),
                weather.getTempF(),
                weather.getVisibility(),
                weather.getWindDir16Point(),
                weather.getWindDirDegree(),
                weather.getWindSpeedKmph(),
                weather.getWindspeedMiles(),
                weather.getWeatherDesc(),
                weather.getWeatherIconUrl()});
    }

    public CurrentWeather getCurrentWeather(long pinId)  {
        CurrentWeather weather;
        StringBuilder sbSelect = new StringBuilder();
        sbSelect.append("SELECT num_cloud_cover ");
        sbSelect.append(     ", num_humidity ");
        sbSelect.append(     ", num_precipitation_mm ");
        sbSelect.append(     ", num_pressure ");
        sbSelect.append(     ", num_temp_c ");
        sbSelect.append(     ", num_temp_f ");
        sbSelect.append(     ", num_visibility ");
        sbSelect.append(     ", txt_wind_dir_16Point ");
        sbSelect.append(     ", txt_wind_dir_degree ");
        sbSelect.append(     ", num_wind_speed_kmph ");
        sbSelect.append(     ", num_wind_speed_mph ");
        sbSelect.append(     ", txt_description ");
        sbSelect.append(     ", txt_icon_url ");
        sbSelect.append(  "FROM PIN_WEATHER ");
        sbSelect.append( "WHERE id_pin = ? ");
        Cursor cursorWeather = database.rawQuery(sbSelect.toString(), new String[]{String.valueOf(pinId)});
        cursorWeather.moveToFirst();
        if(cursorWeather.isAfterLast()) return null;
        weather = cursorToCurrentWeather(cursorWeather);
        return weather;
    }

    private CurrentWeather cursorToCurrentWeather(Cursor cursor) {
        CurrentWeather currentWeather = new CurrentWeather();
        currentWeather.setCloudCover(cursor.getDouble(0));
        currentWeather.setHumidity(cursor.getDouble(1));
        currentWeather.setPrecipMM(cursor.getDouble(2));
        currentWeather.setPressure(cursor.getDouble(3));
        currentWeather.setTempC(cursor.getDouble(4));
        currentWeather.setTempF(cursor.getDouble(5));
        currentWeather.setVisibility(cursor.getDouble(6));
        currentWeather.setWindDir16Point(cursor.getString(7));
        currentWeather.setWindDirDegree(cursor.getInt(8));
        currentWeather.setWindSpeedKmph(cursor.getInt(9));
        currentWeather.setWindspeedMiles(cursor.getInt(10));
        currentWeather.setWeatherDesc(cursor.getString(11));
        currentWeather.setWeatherIconUrl(cursor.getString(12));



        return currentWeather;
    }


    public CurrentWeather getCurrentWeatherToSync(long pinId) {
        CurrentWeather weather;
        StringBuilder sbSelect = new StringBuilder();
        sbSelect.append("SELECT num_cloud_cover ");
        sbSelect.append(     ", num_humidity ");
        sbSelect.append(     ", num_precipitation_mm ");
        sbSelect.append(     ", num_pressure ");
        sbSelect.append(     ", num_temp_c ");
        sbSelect.append(     ", num_temp_f ");
        sbSelect.append(     ", num_visibility ");
        sbSelect.append(     ", txt_wind_dir_16Point ");
        sbSelect.append(     ", txt_wind_dir_degree ");
        sbSelect.append(     ", num_wind_speed_kmph ");
        sbSelect.append(     ", num_wind_speed_mph ");
        sbSelect.append(     ", txt_description ");
        sbSelect.append(     ", txt_icon_url ");
        sbSelect.append(  "FROM PIN_WEATHER ");
        sbSelect.append( "WHERE id_pin = ? ");
        sbSelect.append( "  and txt_synced = 'N' ");
        Cursor cursorWeather = database.rawQuery(sbSelect.toString(), new String[]{String.valueOf(pinId)});
        cursorWeather.moveToFirst();
        if(cursorWeather.isAfterLast()) return null;
        weather = cursorToCurrentWeather(cursorWeather);
        return weather;
    }

    public CurrentWeather getCurrentWeatherSynced(long pinId) {
        CurrentWeather weather;
        StringBuilder sbSelect = new StringBuilder();
        sbSelect.append("SELECT num_cloud_cover ");
        sbSelect.append(     ", num_humidity ");
        sbSelect.append(     ", num_precipitation_mm ");
        sbSelect.append(     ", num_pressure ");
        sbSelect.append(     ", num_temp_c ");
        sbSelect.append(     ", num_temp_f ");
        sbSelect.append(     ", num_visibility ");
        sbSelect.append(     ", txt_wind_dir_16Point ");
        sbSelect.append(     ", txt_wind_dir_degree ");
        sbSelect.append(     ", num_wind_speed_kmph ");
        sbSelect.append(     ", num_wind_speed_mph ");
        sbSelect.append(     ", txt_description ");
        sbSelect.append(     ", txt_icon_url ");
        sbSelect.append(  "FROM PIN_WEATHER ");
        sbSelect.append( "WHERE id_pin = ? ");
        sbSelect.append( "  and txt_synced = 'Y' ");
        Cursor cursorWeather = database.rawQuery(sbSelect.toString(), new String[]{String.valueOf(pinId)});
        cursorWeather.moveToFirst();
        if(cursorWeather.isAfterLast()) return null;
        weather = cursorToCurrentWeather(cursorWeather);
        return weather;
    }
}
